package cn.fastoo.web.api.controller.user;


import cn.fastoo.common.dao.UserAccountDao;
import cn.fastoo.common.entity.UserAccount;
import cn.fastoo.common.entity.UserBase;
import cn.fastoo.common.entity.UserSmstemplate;
import cn.fastoo.common.sampleframe.SFBeanUtils;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.utils.Constant;
import cn.fastoo.service.UserService;
import cn.fastoo.web.dto.UserSignatureDto;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 签名
 * @author daniel.chan
 */
@Controller("signatureController")
@RequestMapping("/v1/admin")
public class SignatureController {

	private final Logger logger = LoggerFactory.getLogger(SignatureController.class);

	@Autowired
	private UserAccountDao userAccountDao;
	@Autowired
	private UserService userService;


	@RequestMapping(value = "signature")
	@ResponseBody
	public String signatureUI(UserBase userBase,String signName) {
		Search search = new Search();
		search.addSearchFilter("EQ_userBaseId",userBase.getId());
		if (StringUtils.isNotBlank(signName)) {
			search.addSearchFilter("EQ_signName", signName);
		} else {
			search.addSearchFilter("NEQ_signName", "");
		}

		List<UserAccount> userAccounts = userAccountDao.find(search);

		JSONObject jsonObject = new JSONObject();
		List<UserSignatureDto> list = new ArrayList<UserSignatureDto>();
		for (UserAccount userAccount : userAccounts) {
			list.add(SFBeanUtils.injectByObject(new UserSignatureDto(),userAccount));
		}

		jsonObject.put("userSignatures",list);
		return new JsonResult(0,"ok",jsonObject).toJson(SerializerFeature.WriteMapNullValue);
	}

	/**
	 * 申请签名
	 */
	@RequestMapping(value = "signature/save")
	@ResponseBody
	public String signatureSave(@RequestParam String selectApiKey,@RequestParam String signName) throws Exception {
		UserAccount userAccount = userAccountDao.findOne(new Search().addSearchFilter("EQ_apiKey",selectApiKey));
		if (userAccount==null) {
			return JsonResult.create(301,"非法访问");
		}
		if (StringUtils.isNotBlank(userAccount.getSignName())&&userAccount.getSignName().equals(signName)) {
			return JsonResult.create(302,"已经为apiKey设置过签名");
		}
		userAccount.setSignStatus(Constant.SignStatus.UnSign.getValue());
		userAccount.setSignName(signName);
		userAccount.setSignApplyTime(new Date());
		userAccountDao.update(userAccount);
		return JsonResult.success("保存成功");
	}

	/**
	 * 删除签名
	 */
	@RequestMapping(value = "signature/del")
	@ResponseBody
	public String delsignature(@RequestParam String selectApiKey) {
		return userService.delSignature(selectApiKey);
	}

}
